package com.erp.module.crm.sSCrm

import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.activity.SeekActivity
import com.erp.bean.CrmSouSuoBean
import com.erp.R
import com.erp.activity.MainKActivity
import com.erp.bean.Twostringbean
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.module.crm.FragmentCrm
import com.erp.popupwin.PopShejiXiaoshouDuoxuan
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.activity_crm_sousuo.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import java.util.*
import kotlin.collections.ArrayList


/**
 * Created by Administrator on 2016/11/19/019.
 * 作者：刘洋
 */
class CrmSouSuoKActivity : BaseActivity(), View.OnClickListener {
    private val shengfen = arrayOf("", "河北", "辽宁", "北京", "黑龙江", "吉林", "香港", "澳门", "台湾", "陕西", "甘肃", "宁夏", "青海", "新疆", "重庆", "四川", "贵州", "云南", "西藏", "海南", "广西", "广东", "天津", "山西", "内蒙古", "湖南", "河南", "湖北", "江苏", "浙江", "上海", "山东", "山西", "福建", "安徽", "海外")
    lateinit var mtefu : MutableList<String>
    lateinit var souSuoBean : CrmSouSuoBean
    var xiaoshouyuan_uid =""
    var shejishi_uid=""
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_crm_sousuo
    }

    override fun initCreate() {
        initView()
        initData()
    }
    private fun initView() {
        souSuoBean= FragmentCrm.souSuoBean
        Header.tv_header_title.text = "属性筛选"
        Header.iv_header_left.visibility=View.VISIBLE
        Header.iv_header_left.setOnClickListener(this)
        TvXiaoshouyuan.setOnClickListener(this)
        TvShejishi.setOnClickListener(this)
        TvKehuming.setOnClickListener(this)
        TvShoujihao.setOnClickListener(this)
        TvShengchan.setOnClickListener(this)
        TvHetong.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
//        tv_zhuanmaidian.setOnClickListener(this)
//        tvTeFu.setOnClickListener(this)
//        if(MainKActivity.ZhuanMaiDianid =="0"){
//            LlTeFu.visibility=View.VISIBLE
//        }else{
//            LlTeFu.visibility=View.GONE
//        }
        BtQueding.setOnClickListener(this)
        EtSouyou.isFocusable = true
        EtSouyou.isFocusableInTouchMode = true
        EtSouyou.requestFocus()
        EtSouyou.requestFocusFromTouch()
    }
    private fun initData() {
        when {
            souSuoBean.time_category == "crm_o.ctime" -> SpLeibieTime.setSelection(1)
            souSuoBean.time_category == "crm_o.sqpud_time" -> SpLeibieTime.setSelection(2)
            souSuoBean.time_category == "crm_o.sqpad_time" -> SpLeibieTime.setSelection(3)
            souSuoBean.time_category== "crm_o.sqxd_time" -> SpLeibieTime.setSelection(4)
            souSuoBean.time_category== "o.cd_time" -> SpLeibieTime.setSelection(5)
            souSuoBean.time_category== "crm_o.measuring_time" -> SpLeibieTime.setSelection(6)
//            souSuoBean.time_category== "o.quoted_price_time" -> SpLeibieTime.setSelection(7)
//            souSuoBean.time_category== "o.dingbao_time" -> SpLeibieTime.setSelection(8)
//            souSuoBean.time_category== "o.charge_audit_time" -> SpLeibieTime.setSelection(9)
//            souSuoBean.time_category== "o.warehousing_time" -> SpLeibieTime.setSelection(10)
//            souSuoBean.time_category== "o.deliver_time" -> SpLeibieTime.setSelection(11)
        }
        EtSouyou.setText(souSuoBean.all)
        TvXiaoshouyuan.text =souSuoBean.xiaoshouyuan
        TvShejishi.text=souSuoBean.shejishi
        TvKehuming.text=souSuoBean.kehuming
        TvShoujihao.text=souSuoBean.shoujihao
        TvShengchan.text=souSuoBean.shengchan
        TvHetong.text=souSuoBean.hetong
        TvStartTime.text = souSuoBean.start_time
        TvEndTime.text = souSuoBean.end_time




//        val shengfens = ArrayList(Arrays.asList(*shengfen))
//        val shengfenadapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, shengfens)
//        sp_shengfen.adapter = shengfenadapter
//
//        tv_zhuanmaidian.text = souSuoBean.shop_name
//        for (j in shengfen.indices) {
//            if (shengfen[j] == souSuoBean.province) {
//                sp_shengfen.setSelection(j)
//                break
//            }
//        }
//        val quyu = arrayOf("1", "5", "7", "6", "8", "4", "3", "2", "11", "9", "10")
//        for (j in quyu.indices) {
//            if (quyu[j] == souSuoBean.area) {
//                sp_quyu.setSelection(j + 1)
//                break
//            }
//        }
//        val quy = arrayOf("12", "9", "15", "16", "10")
//        for (j in quy.indices) {
//            if (quy[j] == souSuoBean.o_type) {
//                sp_leixing!!.setSelection(j + 1)
//                break
//            }
//        }
//        val quyu1 = arrayOf("yes", "no")
//        for (j in quyu1.indices) {
//            if (quyu1[j] == souSuoBean.is_canceled) {
//                sp_zhaungtai.setSelection(j + 1)
//                break
//            }
//        }
//        val quyu2 = arrayOf("cd_time", "quoted_price_time", "dingbao_time", "charge_audit_time", "warehousing_time", "deliver_time")
//        for (j in quyu2.indices) {
//            if (quyu2[j] == souSuoBean.field_empty) {
//                sp_ifnull.setSelection(j + 1)
//                break
//            }
//        }
//        val quyu3 = arrayOf("cd_time", "quoted_price_time", "dingbao_time", "charge_audit_time", "warehousing_time", "deliver_time")
//        for (j in quyu3.indices) {
//            if (quyu3[j] == souSuoBean.field_not_empty) {
//                sp_ifnonull.setSelection(j + 1)
//                break
//            }
//        }

//        mtefu=souSuoBean.special_service_cat
//        if(mtefu.size>0) {
//            val tefu = StringBuffer("")
//            for (j in 0 until mtefu.size) {
//                if (j == mtefu.size - 1) {
//                    tefu.append(mtefu[j])
//                } else {
//                    tefu.append(mtefu[j])
//                    tefu.append(" ")
//                }
//            }
//            Myutil.settv(tefu.toString(), tvTeFu)
//        }
    }

    override fun onClick(v: View?) {
        when(v){
            Header.iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            TvXiaoshouyuan -> getintent(SeekActivity.SEEK_XIAOSHOUYUAN, 1,"销售员",TvXiaoshouyuan.text.toString())
            TvShejishi -> getintent(SeekActivity.SEEK_SHEJISHI, 2,"设计师",TvShejishi.text.toString())
            TvKehuming -> getintent(SeekActivity.SEEK_KEHUMING, 3,"客户名",TvKehuming.text.toString())
            TvShoujihao -> getintent(SeekActivity.SEEK_TEL, 4,"手机号",TvShoujihao.text.toString())
            TvShengchan -> getintent(SeekActivity.SEEK_ORDER, 5,"生产单号",TvShengchan.text.toString())
            TvHetong -> getintent(SeekActivity.SEEK_HETONG, 6,"合同号",TvHetong.text.toString())
            TvStartTime -> {
                Myutil.notjianpan(this@CrmSouSuoKActivity,Header.iv_header_left)
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), this@CrmSouSuoKActivity)
            }
            TvEndTime -> {
                Myutil.notjianpan(this@CrmSouSuoKActivity,Header.iv_header_left)
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime.text.toString(), this@CrmSouSuoKActivity)
            }
            BtQueding -> {
                getkey()
                val intent = Intent()
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
//            tvTeFu -> {
//                val pop = PopShejiXiaoshouDuoxuan(this@CrmSouSuoKActivity,PopShejiXiaoshouDuoxuan.TF, souSuoBean.special_service_cat, object : PopShejiXiaoshouDuoxuan.duoxuan {
//                    override fun xuanze(list: MutableList<String>, listid: MutableList<String>) {
//                        val shejishi = StringBuffer("")
//                        for (j in 0 until list.size) {
//                            if (j == list.size - 1) {
//                                shejishi.append(list[j])
//                            } else {
//                                shejishi.append(list[j])
//                                shejishi.append(" ")
//                            }
//                        }
//                        Myutil.settv(shejishi.toString(), tvTeFu)
//                        mtefu = list
//                    }
//
//                }, "请选择销售员")
//                pop.show(Sputil.sp("erp"), tvTeFu)
//            }
//            tv_zhuanmaidian -> {
//                getintent(SeekActivity.SEEK_ZMD, 10,"专卖店",tv_zhuanmaidian.text.toString())
//            }
        }
    }
    private fun getintent(select: Int, requestCode: Int,title : String,data : String) {
        val intent = Intent()
        intent.setClass(this@CrmSouSuoKActivity, SeekActivity::class.java)
        intent.putExtra("select", select)
        intent.putExtra("title",title)
        intent.putExtra("data",data)
        startActivityForResult(intent, requestCode)
        overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
    }


    /**
     * @return  获得搜索词条
     */
    private fun getkey(){
        val timenanme = SpLeibieTime.selectedItem.toString()
        if (timenanme == "创建时间") {
            souSuoBean.time_category = "crm_o.ctime"
        } else if (timenanme == "审图申请时间") {
            souSuoBean.time_category = "crm_o.sqpud_time"
        } else if (timenanme == "传图时间") {
            souSuoBean.time_category = "crm_o.sqpad_time"
        } else if (timenanme == "下单申请时间") {
            souSuoBean.time_category = "crm_o.sqxd_time"
        } else if (timenanme == "下单受理时间") {
            souSuoBean.time_category = "o.cd_time"
        } else if (timenanme == "量尺时间") {
            souSuoBean.time_category = "crm_o.measuring_time"
        }else if(timenanme == "报价时间"){
            souSuoBean.time_category = "o.quoted_price_time"
        }else if(timenanme == "定包时间"){
            souSuoBean.time_category = "o.dingbao_time"
        }else if(timenanme == "收款时间"){
            souSuoBean.time_category = "o.charge_audit_time"
        }else if(timenanme == "入仓时间"){
            souSuoBean.time_category = "o.warehousing_time"
        }else if(timenanme == "发货时间"){
            souSuoBean.time_category = "o.deliver_time"
        }
        souSuoBean.all=EtSouyou.text.toString()
        souSuoBean.start_time=TvStartTime.text.toString()
        souSuoBean.end_time=TvEndTime.text.toString()
        souSuoBean.xiaoshouyuan=TvXiaoshouyuan.text.toString()
        souSuoBean.xiaoshouyuan_uid=xiaoshouyuan_uid
        souSuoBean.shejishi=TvShejishi.text.toString()
        souSuoBean.shejishi_uid=shejishi_uid
        souSuoBean.kehuming=TvKehuming.text.toString()
        souSuoBean.shoujihao=TvShoujihao.text.toString()
        souSuoBean.shengchan=TvShengchan.text.toString()
        souSuoBean.hetong=TvHetong.text.toString()
//        souSuoBean.shop_name=tv_zhuanmaidian.text.toString()
//        souSuoBean.special_service_cat=mtefu
//        souSuoBean.province = sp_shengfen.selectedItem.toString()
//        getquyu()//获得区域参数
//        getleixing()//获得单类型参数
//        getzhaungtai()//获得书否取消状态参数
//        getisnull()//获得属于空的参数
//        getisnonull()//获得非空的参数
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                xiaoshouyuan_uid= data.getStringExtra("uid")
                TvXiaoshouyuan.text = s
            }
            2 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                shejishi_uid= data.getStringExtra("uid")
                TvShejishi.text = s
            }
            3 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                TvKehuming.text = s
            }
            4 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                TvShoujihao.text = s
            }
            5 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                TvShengchan.text = s
            }
            6 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                TvHetong.text = s
            }
//            10 -> if (resultCode == Activity.RESULT_OK) {
//                val s = data.getStringExtra("name")
//                tv_zhuanmaidian.text = s
//            }
        }
    }

//    private fun getquyu() {
//        if (sp_quyu.selectedItem.toString() !== "") {
//            when (sp_quyu.selectedItem.toString()) {
//                "东北区" -> {
//                    souSuoBean.area = "1"
//                    souSuoBean.areas = "东北区"
//                }
//                "华东一区" -> {
//                    souSuoBean.area = "5"
//                    souSuoBean.areas = "华东一区"
//                }
//                "华东三区" -> {
//                    souSuoBean.area = "7"
//                    souSuoBean.areas = "华东三区"
//                }
//                "华东二区" -> {
//                    souSuoBean.area = "6"
//                    souSuoBean.areas = "华东二区"
//                }
//                "华南区" -> {
//                    souSuoBean.area = "8"
//                    souSuoBean.areas = "华南区"
//                }
//                "华总二区" -> {
//                    souSuoBean.area = "4"
//                    souSuoBean.areas = "华总二区"
//                }
//                "华中一区" -> {
//                    souSuoBean.area = "3"
//                    souSuoBean.areas = "华中一区"
//                }
//                "华北区" -> {
//                    souSuoBean.area = "2"
//                    souSuoBean.areas = "华北区"
//                }
//                "直营区" -> {
//                    souSuoBean.area = "11"
//                    souSuoBean.areas = "直营区"
//                }
//                "西北区" -> {
//                    souSuoBean.area = "9"
//                    souSuoBean.areas = "西北区"
//                }
//                "西南区" -> {
//                    souSuoBean.area = "10"
//                    souSuoBean.areas = "西南区"
//                }
//            }
//        }
//    }
//
//    //获得书否取消状态参数
//    private fun getzhaungtai() {
//        if (sp_zhaungtai.selectedItem.toString() !== "") {
//            val xsy = Twostringbean()
//            when (sp_zhaungtai.selectedItem.toString()) {
//                "是" -> souSuoBean.is_canceled = "yes"
//                "否" -> souSuoBean.is_canceled = "no"
//            }
//        }
//    }
//
//    //获得非空的参数
//    private fun getisnonull() {
//        if (sp_ifnonull!!.selectedItem.toString() !== "") {
//            val xsy = Twostringbean()
//            when (sp_ifnonull!!.selectedItem.toString()) {
//                "" -> {
//                    souSuoBean.field_not_empty = ""
//                    souSuoBean.field_not_emptys = ""
//                }
//                "下单受理时间" -> {
//                    souSuoBean.field_not_empty = "cd_time"
//                    souSuoBean.field_not_emptys = "下单受理时间"
//                }
//                "报价时间" -> {
//                    souSuoBean.field_not_empty = "quoted_price_time"
//                    souSuoBean.field_not_emptys = "报价时间"
//                }
//                "定包时间" -> {
//                    souSuoBean.field_not_empty = "dingbao_time"
//                    souSuoBean.field_not_emptys = "定包时间"
//                }
//                "收款时间" -> {
//                    souSuoBean.field_not_empty = "charge_audit_time"
//                    souSuoBean.field_not_emptys = "收款时间"
//                }
//                "入仓时间" -> {
//                    souSuoBean.field_not_empty = "warehousing_time"
//                    souSuoBean.field_not_emptys = "入仓时间"
//                }
//                "发货时间" -> {
//                    souSuoBean.field_not_empty = "deliver_time"
//                    souSuoBean.field_not_emptys = "发货时间"
//                }
//            }
//        }
//    }
//
//    //获得属于空的参数
//    private fun getisnull() {
//        if (sp_ifnull.selectedItem.toString() != "") {
//            when (sp_ifnull.selectedItem.toString()) {
//                "" -> {
//                    souSuoBean.field_emptys = ""
//                    souSuoBean.field_emptys = ""
//                }
//                "下单受理时间" -> {
//                    souSuoBean.field_empty = "cd_time"
//                    souSuoBean.field_emptys = "下单受理时间"
//                }
//                "报价时间" -> {
//                    souSuoBean.field_empty = "quoted_price_time"
//                    souSuoBean.field_emptys = "报价时间"
//                }
//                "定包时间" -> {
//                    souSuoBean.field_empty = "dingbao_time"
//                    souSuoBean.field_emptys = "定包时间"
//                }
//                "收款时间" -> {
//                    souSuoBean.field_empty = "charge_audit_time"
//                    souSuoBean.field_emptys = "收款时间"
//                }
//                "入仓时间" -> {
//                    souSuoBean.field_empty = "warehousing_time"
//                    souSuoBean.field_emptys = "入仓时间"
//                }
//                "发货时间" -> {
//                    souSuoBean.field_empty = "deliver_time"
//                    souSuoBean.field_emptys = "发货时间"
//                }
//            }
//        }
//    }
//
//    //获得单类型参数
//    private fun getleixing() {
//        if (sp_leixing.selectedItem.toString() !== "") {
//            val xsy = Twostringbean()
//            when (sp_leixing.selectedItem.toString()) {
//                "A单" -> {
//                    souSuoBean.o_type = "12"
//                    souSuoBean.o_types = "A单"
//                }
//                "B单" -> {
//                    souSuoBean.o_type = "9"
//                    souSuoBean.o_types = "B单"
//                }
//                "C单" -> {
//                    souSuoBean.o_type = "15"
//                    souSuoBean.o_types = "C单"
//                }
//                "E单" -> {
//                    souSuoBean.o_type = "16"
//                    souSuoBean.o_types = "E单"
//                }
//                "直发" -> {
//                    souSuoBean.o_type = "10"
//                    souSuoBean.o_types = "直发"
//                }
//            }
//
//        }
//    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == 4) {
            finish()
            overridePendingTransition(0, R.anim.end_activity1)
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        private val a = "PropertyActivity"
    }
}
